using IM.Easy.Infrastructure.Query;
using Microsoft.AspNetCore.Mvc;
using IM.Easy.SystemServices.Dtos;
using IM.Easy.SystemServices.IServices;
using IM.Easy.Entity.System;
using IM.Easy.Core.Attributes;
using IM.Easy.SystemServices.Dtos.User;
using IM.Easy.SystemServices.Dtos.Role;

namespace IM.Easy.Api.Controllers.System
{
    /// <summary>
    /// SysRoleController 控制器
    /// </summary>
    [Route("system/role")]
    public class RoleController : ControllerBase
    {
        private readonly IRoleService _service;

        /// <summary>
        /// 构造函数，注入服务
        /// </summary>
        /// <param name="service">服务接口</param>
        public RoleController(IRoleService service)
        {
            _service = service;

        }

        /// <summary>
        /// 获取所有SysRole对象
        /// </summary>
        /// <returns>返回SysRole列表</returns>
        [HttpGet]
        [Api(Name = "获取角色列表", Perm = "role:list")]
        public PageResponse<SysRole> GetList([FromQuery] SysRoleQueryDto query)
        {
            return _service.GetPageList(query);
        }

        /// <summary>
        /// 根据ID获取SysRole对象
        /// </summary>
        /// <param name="id">对象的ID</param>
        /// <returns>返回指定的SysRole对象</returns>
        [HttpGet("{id}")]
        [Api(Name = "获取角色明细", Perm = "role:info")]
        public SysRole GetById([FromRoute] long id)
        {
            var result = _service.GetById(id);
            return result ?? throw new KeyNotFoundException($"未找到ID为 {id} 的SysRole对象。");
        }

        /// <summary>
        /// 创建新的SysRole对象
        /// </summary>
        /// <param name="entity">要创建的SysRole对象</param>
        /// <returns>返回创建后的SysRole对象</returns>
        [HttpPost]
        [Api(Name = "添加角色", Perm = "role:add")]
        public SysRole Create([FromBody] SysRole entity)
        {
            _service.Create(entity);
            return entity; // 直接返回创建的实体
        }

        /// <summary>
        /// 更新现有的SysRole对象
        /// </summary>
        /// <param name="id">对象的ID</param>
        /// <param name="entity">更新后的SysRole对象</param>
        [HttpPut]
        [Api(Name = "修改角色", Perm = "role:put")]
        public void Update([FromBody] SysRole entity)
        {
            _service.UpdateRole(entity);
        }

        /// <summary>
        /// 批量删除SysRole对象
        /// </summary>
        /// <param name="ids">对象的IDS</param>
        [HttpDelete]
        [Api(Name = "删除角色", Perm = "role:del:bulk")]
        public void Delete([FromBody] params long[] ids)
        {
            _service.DeleteRole(ids);
        }

        /// <summary>
        /// 删除角色和相关权限及用户
        /// </summary>
        /// <param name="id"></param>
        [HttpDelete("{id}")]
        [Api(Name = "删除角色", Perm = "role:del")]
        public void Delete([FromRoute] long id)
        {
            _service.DeleteRole(id);
        }

        /// <summary>
        /// 修改启用状态
        /// </summary>
        /// <param name="status"></param>
        /// <returns></returns>
        [HttpPut]
        [Api(Name = "修改角色状态", Perm = "role:status")]
        public bool ChangeStatus([FromBody] RoleStatusDto data)
        {
            var role = GetById(data.Id);
            role.IsActive = data.IsActive;
            return _service.Update(role);
        }


        /// <summary>
        /// 获取未分配用户
        /// </summary>
        /// <returns></returns>
        [HttpGet("authUser/unassign")]
        [Api(Name = "获取未分配用户", Perm = "role:user:unassigned")]
        public PageResponse<UserDto> GetUnAssignUser(UserRoleQueryDto query)
        {
            var page = _service.GetUsersNotIn(query);
            return page;
        }

        /// <summary>
        /// 获取已为角色分配的用户
        /// </summary>
        /// <returns></returns>
        [HttpGet("authUser/assigned")]
        [Api(Name = "获取已分配用户", Perm = "role:user:assigned")]
        public PageResponse<UserDto> GetAssignedUser(UserRoleQueryDto query)
        {
            var page = _service.GetUsers(query);
            return page;
        }

        /// <summary>
        /// 分配用户
        /// </summary>
        /// <param name="id"></param>
        /// <param name="uid"></param>
        [HttpPut("authUser/assign/{id}")]
        [Api(Name = "分配用户", Perm = "role:user:assign")]
        public void AssignUser([FromRoute] long id, [FromBody] params long[] uid)
        {
            _service.SetRole(id, uid);
        }

        /// <summary>
        /// 取消角色授权
        /// </summary>
        /// <param name="id"></param>
        /// <param name="uid"></param>
        [HttpPut("authUser/cancel/{id}")]
        [Api(Name = "取消角色授权", Perm = "role:user:unassign")]
        public void Cancel([FromRoute] long id, [FromBody] params long[] uid)
        {
            _service.RemoveRole(id, uid);
        }
    }
}
